package com.oa.ylms.apps.dao.assess.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.assess.IAssessModelStates;
import com.oa.ylms.apps.dao.assess.IAssessModelDao;
import com.oa.ylms.apps.entity.assess.AssessModelValue;

@SuppressWarnings("unchecked")
public class AssessModelDaoImpl extends HibernateDaoSupport implements
		IAssessModelDao {

	private List<AssessModelValue> list;
	private int numbers;

	public int addAssessModel(AssessModelValue assessModel) {
		if (assessModel != null) {
			this.getHibernateTemplate().save(assessModel);
		}
		return assessModel.getModelId();
	}

	public boolean editAssessModel(AssessModelValue assessModel) {
		if (assessModel != null) {
			try {
				this.getHibernateTemplate().update(assessModel);
			} catch (Exception e) {
				return false;
			}
			return true;
		}
		return false;
	}

	public List<AssessModelValue> query() {
		return this.getHibernateTemplate().find(
				"from AssessModelValue where states=?", IDEDStates.ENABLE);
	}

	public List<AssessModelValue> queryByModelName(AssessModelValue assessModel) {

		return this.getHibernateTemplate().find(
				"from AssessModelValue where modelName =? ",
				assessModel.getModelName());
	}

	public AssessModelValue queryAssessModelByModelId(int modelId) {

		return (AssessModelValue) this.getHibernateTemplate().get(
				AssessModelValue.class, modelId);
	}

	public int countTpages(final AssessModelValue assessModel, final int rows)
			throws Exception {
		int pageNumbers = 0;
		String hqlStr = "select amv from AssessModelValue amv where amv.states <> ? ";
		if (assessModel.getModelName() != null) {
			hqlStr += " AND amv.modelName =?  ";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				int index = 0;
				Query query = session.createQuery(hql);
				query.setInteger(index, IAssessModelStates.DELETE);
				if (assessModel.getModelName() != null) {
					index++;
					query.setString(index, assessModel.getModelName());
				}
				list = query.list();
				return list;
			}
		});
		this.numbers = list.size();
		if (numbers <= rows) {
			pageNumbers = 1;
			if (numbers == 0) {
				pageNumbers = 0;
			}
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;
	}

	public List<AssessModelValue> queryAssessModelWithPage(
			final AssessModelValue assessModel, final int rows,
			final int pageNo) throws Exception {
		String hqlStr = "select amv from AssessModelValue amv where amv.states <> ? ";
		if (assessModel.getModelName() != null) {
			hqlStr += " AND amv.modelName =?  ";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				int index = 0;
				Query query = session.createQuery(hql);
				query.setInteger(index, IAssessModelStates.DELETE);
				if (assessModel.getModelName() != null) {
					index++;
					query.setString(index, assessModel.getModelName());
				}
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}
	
	public List<AssessModelValue> queryAllAssessModel() throws Exception {
		final String hql = "select amv from AssessModelValue amv where amv.states = ?";
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				int index = 0;
				Query query = session.createQuery(hql);
				query.setInteger(index, IAssessModelStates.ENABLE);
				list = query.list();
				return list;
			}
		});
		return list;
	}
	
	public int getTotalNumber() throws Exception {
		
		return numbers;
	}


	public List<AssessModelValue> getList() {
		return list;
	}

	public void setList(List<AssessModelValue> list) {
		this.list = list;
	}

	public int getNumbers() {
		return numbers;
	}

	public void setNumbers(int numbers) {
		this.numbers = numbers;
	}

}
